package org.lanqiao.music.system.service.impl;

import org.lanqiao.music.common.exp.*;
import org.lanqiao.music.common.dto.LoginDTO;
import org.lanqiao.music.system.entity.User;
import org.lanqiao.music.system.dao.UserDao;
import org.lanqiao.music.system.service.UserService;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageHelper;
import org.lanqiao.music.common.utils.PageDTO;
import javax.annotation.Resource;

/**
 * (User)表服务实现类
 *
 * @author makejava
 * @since 2024-08-15 20:54:51
 */
@Service("userService")
@CacheConfig(cacheNames = "user1")
public class UserServiceImpl implements UserService {
    @Resource
    private UserDao userDao;

    /**
     * 通过ID查询单条数据
     *
     * @param userId 主键
     * @return 实例对象
     */
    @Override
    @Cacheable(key = "#userId")
    public User queryById(Integer userId) {
        System.out.println("调用方法查找");
        return this.userDao.queryById(userId);
    }

    /**
     * 分页查询
     *
     * @param user 筛选条件
     * @return 查询结果
     */
    @Override
    public PageInfo<User> queryByPage(User user, PageDTO pageDTO) {
        PageHelper.startPage(pageDTO.getPageNum(),pageDTO.getPageSize());
        PageInfo page = new PageInfo<>(this.userDao.queryAllByLimit(user));
        return page;
    }

    /**
     * 新增数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @Override
    public User insert(User user) {
        if (user.getUserName()==null || user.getUserName().equals("")){
            throw new UsernameNullException("用户名不能为空");
        }else if(user.getPassword()==null || user.getPassword().equals("")){
            throw new PasswordNullException("密码不能为空");
        }else if(user.getSex()==null || user.getSex().equals("")) {
            throw new SexNullException("性别不能为空");
        }else if(user.getNickName()==null || user.getNickName().equals("")) {
            throw new NickNameNullException("昵称不能为空");
        }
        this.userDao.insert(user);
        return user;
    }

    /**
     * 修改数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @Override
    public User update(User user) {
        this.userDao.update(user);
        return this.queryById(user.getUserId());
    }

    /**
     * 通过主键删除数据
     *
     * @param userId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer userId) {
        return this.userDao.deleteById(userId) > 0;
    }

    @Override
    public User Login(LoginDTO dto,Object code) {
        if(dto.getCode() == null || dto.getCode().equals("")){
            throw new CaptchaNullException("验证码为空");
        }
        if (code == null){
            throw new CaptchaExpiredException("验证码过期");
        }
        if (!code.equals(dto.getCode())){
            throw new CaptchaErrorException("验证码错误");
        }
        User user = userDao.findUserByName(dto.getUsername());
        if (user==null){
            System.out.println("用户不存在");
            throw new AccountNullException("用户不存在,请先注册！！");
        }
        if (!dto.getPassword().equals(user.getPassword())){
            System.out.println("密码错误");
            throw new AccountErrorException("密码错误");
        }
        return user;

    }


}
